﻿Imports System.Web.Configuration

Public Class oGeneral
    Public Function GenerateUDT_UDF() As String
        Try
            Dim ret As String = ""
            ret = CreateUserCodeField()

            Return ret
        Catch ex As Exception
            Return ex.ToString
        End Try
    End Function
    Private Function CreateUserCodeField() As String
        Try
            Dim ret As String = ""

            ret = oXML.ConnectSAP()
            If ret = "" Then
                ret = CreateUDF("ORDR", "UserCode", "User Code", SAPbobsCOM.BoFieldTypes.db_Alpha, SAPbobsCOM.BoFldSubTypes.st_None, 10)
                If ret = "" Then
                    ret = CreateUDF("OCRD", "UserCode", "User Code", SAPbobsCOM.BoFieldTypes.db_Alpha, SAPbobsCOM.BoFldSubTypes.st_None, 10)
                    If ret = "" Then
                        ret = CreateUDF("OCLG", "UserCode", "User Code", SAPbobsCOM.BoFieldTypes.db_Alpha, SAPbobsCOM.BoFldSubTypes.st_None, 10)
                        If ret = "" Then
                            ret = CreateUDF("OOPR", "UserCode", "User Code", SAPbobsCOM.BoFieldTypes.db_Alpha, SAPbobsCOM.BoFldSubTypes.st_None, 10)
                        End If
                    End If
                End If
            End If

            Return ret
        Catch ex As Exception
            Return ex.ToString
        End Try
    End Function
    Private Function CreateUDF(ByVal tableName As String, ByVal fieldName As String, ByVal desc As String, _
                               ByVal fieldType As SAPbobsCOM.BoFieldTypes, ByVal subType As SAPbobsCOM.BoFldSubTypes, _
                               ByVal editSize As Integer) As String
        Try
            Dim connect As New Connection()
            Dim fieldID As Long = -1
            Dim ds As DataSet = connect.ObjectGetAll_Query_SAP("SELECT FieldID FROM CUFD WHERE TableID = '" + tableName + "' AND AliasID = N'" + fieldName + "'")
            If ds.Tables.Count > 0 Then
                If ds.Tables(0).Rows.Count > 0 Then
                    fieldID = ds.Tables(0).Rows(0).Item("FieldID")
                End If
            End If

            Dim oUdfMD As SAPbobsCOM.UserFieldsMD = Nothing
            oUdfMD = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oUserFields)

            GC.Collect()
            oUdfMD.TableName = tableName
            oUdfMD.Name = fieldName
            oUdfMD.Type = fieldType
            oUdfMD.Description = desc
            oUdfMD.SubType = subType
            oUdfMD.EditSize = editSize

            Dim lRetCode As Integer = 0

            If oUdfMD.GetByKey(tableName, fieldID) = True Then
                lRetCode = oUdfMD.Update()
            Else
                lRetCode = oUdfMD.Add()
            End If

            System.Runtime.InteropServices.Marshal.ReleaseComObject(oUdfMD)
            oUdfMD = Nothing
            GC.Collect()

            If lRetCode <> 0 Then
                Dim sErrMsg As String = ""
                PublicVariable.oCompany.GetLastError(lRetCode, sErrMsg)
                Return sErrMsg
            Else
                Return ""
            End If
        Catch ex As Exception
            Return ex.ToString
        End Try
    End Function

    Public Function SetConnection(ByVal SQLServer As String, ByVal SQLDBName As String, ByVal SQLUser As String, ByVal SQLPwd As String, ByVal SQLType As String, _
                                   ByVal SAPServer As String, ByVal SAPUser As String, ByVal SAPPwd As String) As String

        Dim ret As String = "Operation Completed"

        Try
            Dim strLocal As String = SQLDBName + ";" + SAPUser + ";" + SAPPwd + ";" + SQLServer + ";" + SQLUser + ";" + SQLPwd + ";" + SAPServer + ";" + SQLType

            Dim config As System.Configuration.Configuration
            config = WebConfigurationManager.OpenWebConfiguration("~/") ' ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None)

            If config.HasFile() Then
                config.AppSettings.Settings.Item("SAPConnect").Value = strLocal
                config.Save(ConfigurationSaveMode.Modified)
                ConfigurationManager.RefreshSection("AppSettings")
            Else
                ret = "Setting file is not found!"
            End If
        Catch ex As Exception
            ret = ex.ToString
        End Try
        Return ret
    End Function

    Public Function TestConnection() As String
        Dim ret As String = "Connect sucessful!"
        ret = oXML.ConnectSAP()
        If ret = "" Then ret = "Connect sucessful!"
        Return ret
    End Function


End Class
